package acme;

import java.util.*;

import javax.jws.*;
import org.hibernate.Session;
import acme.data.*;
import acme.serviceproviders.*;
import acme.serviceproviders.data.*;
import acme.serviceproviders.data.utility.HibernateUtility;

@WebService
public class ACMEWebService {

	@WebMethod
	public UserBooking getBookingDetails(String bookingId) throws Exception
	{
		Session session = HibernateUtility.getSessionFactory().openSession();

		try 
		{
			UserBooking booking = 
				(UserBooking) session.createQuery("from UserBooking where bookingId = ?")
				.setString(0, bookingId)
				.uniqueResult();

			if (booking == null)
				throw new BookingNotFoundException("Booking not found");

			return booking;
		} 
		catch (Exception ex)
		{
			int i = 0;
			throw ex;
		}
		finally 
		{
			session.close();
		}			
	}
	
	@WebMethod
	public TravelPackage[] combineIntoPackages(FlightItinerary[] flights, Apartment[] apartments)
	{
		List<TravelPackage> packageList = new ArrayList<TravelPackage>(); 
		
		for (FlightItinerary flight : flights)
			for (Apartment apartment : apartments) {
				TravelPackage travelPackage = new TravelPackage();
				travelPackage.setFlight(flight);
				travelPackage.setApartment(apartment);

				packageList.add(travelPackage);
			}

		TravelPackage[] result = new TravelPackage[packageList.size()];
		packageList.toArray(result);
		
		return result;
	}	
	
	@WebMethod
	public String createBooking(String apartmentBookingId, String startFlightBookingId, 
			String returnFlightBookingId, String carBookingId, float amountPaid, String username,
			Date startDate, Date returnDate) throws UserNotFoundException
	{
		Session session = HibernateUtility.getSessionFactory().openSession();
		
		try
		{
		User user = (User) 
			session.createQuery("from User where username = :username")
			.setString("username", username)
			.uniqueResult();
		
		if (user == null)
			throw new UserNotFoundException("User not found");
		
		UserBooking booking = new UserBooking();
		booking.setBookingId(UUID.randomUUID().toString());		
		booking.setApartmentBookingId(apartmentBookingId);
		booking.setStartFlightBookingId(startFlightBookingId);
		booking.setReturnFlightBookingId(returnFlightBookingId);
		booking.setAmountPaid(amountPaid);
		booking.setCarBookingId(carBookingId);
		booking.setStartDate(startDate);
		booking.setReturnDate(returnDate);
		booking.setUser(user);
		
		session.save(booking);
		session.flush();
		
		return booking.getBookingId();
		
		}
		finally
		{
			session.close();
		}
	}
	
	@WebMethod
	public void deleteBooking(String bookingId) throws BookingNotFoundException
	{
		Session session = HibernateUtility.getSessionFactory().openSession();
		
		try
		{
		UserBooking booking = (UserBooking) session.createQuery("from UserBooking where bookingId = :bookingId")
		.setString("bookingId", bookingId).uniqueResult();
		
		if (booking == null)
			throw new BookingNotFoundException("Booking not found");
				
		session.delete(booking);
		session.flush();
		}
		finally
		{
			session.close();
		}
	}	
}
